package com.benin.sms.component.dialog

import android.os.Bundle
import android.view.*
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.benin.sms.R
import com.benin.sms.component.button.ConfirmButtonipetrpide

class MessageDialog : DialogFragment() {

    companion object {
        fun showDialogipetrpide(
            fragmentManager: FragmentManager,
            message: String,
            cancel: String = "",
            confirm: String = "OK",
            cancelRes: Int = R.drawable.solid_red500_r4,
            confirmRes: Int = R.drawable.solid_blue500_r4,
            listener: () -> Unit = {},
        ) {
            val dialog = MessageDialog()
            dialog.mMessage = message
            dialog.mCancel = cancel
            dialog.mConfirm = confirm
            dialog.mCancelRes = cancelRes
            dialog.mConfirmRes = confirmRes
            dialog.mOnActionListener = listener
            dialog.showNow(fragmentManager, MessageDialog::class.java.simpleName)
        }
    }

    private lateinit var mMessageDialogRootView: View
    private lateinit var tv_dialogmessage_title: TextView
    private lateinit var cb_dialogmessage_confirm: ConfirmButtonipetrpide
    private lateinit var cb_dialogmessage_cancel: ConfirmButtonipetrpide

    private var mOnActionListener: () -> Unit = {}
    private var mMessage: String? = null
    private var mCancel: String? = null
    private var mConfirm: String? = null
    private var mCancelRes: Int = R.drawable.solid_red500_r4
    private var mConfirmRes: Int = R.drawable.solid_blue500_r4

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);

        mMessageDialogRootView = inflater.inflate(
            R.layout.dialog_message,
            container,
            false
        )
        return mMessageDialogRootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initSubViews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//            it.setWindowAnimations(R.style.bottomDialogAnimation)
        }

        tv_dialogmessage_title.text = mMessage
        cb_dialogmessage_cancel.tv_ipetrpideconfirm_ipetrpidebutton.text = mCancel
        cb_dialogmessage_confirm.tv_ipetrpideconfirm_ipetrpidebutton.text = mConfirm
        cb_dialogmessage_cancel.rl_ipetrpideconfirm_ipetrpidecontent.setBackgroundResource(mCancelRes)
        cb_dialogmessage_confirm.rl_ipetrpideconfirm_ipetrpidecontent.setBackgroundResource(mConfirmRes)

        mCancel?.let {
            if (it.isBlank())
                cb_dialogmessage_cancel.visibility = View.GONE
            else
                cb_dialogmessage_cancel.visibility = View.VISIBLE
        }

        mConfirm?.let {
            if (it.isBlank())
                cb_dialogmessage_confirm.visibility = View.GONE
            else
                cb_dialogmessage_confirm.visibility = View.VISIBLE
        }
    }

    private fun initSubViews() {

        cb_dialogmessage_confirm =
            mMessageDialogRootView.findViewById(R.id.cb_dialogmessage_confirm)

        cb_dialogmessage_cancel =
            mMessageDialogRootView.findViewById(R.id.cb_dialogmessage_cancel)

        tv_dialogmessage_title =
            mMessageDialogRootView.findViewById(R.id.tv_dialogmessage_title)


        cb_dialogmessage_confirm.setOnClickListener {
            mOnActionListener.invoke()
            dismiss()
        }

        cb_dialogmessage_cancel.setOnClickListener {
            dismiss()
        }
    }
}